package com.atguigu.spzx.model.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(title = "响应类")
@Data
public class Result<T> {
    //code值和描述的messsage数据 以后较多 可以通过枚举类维护
    @Schema(description = "响应状态码：200表示成功")
    private Integer code;
    @Schema(description = "响应信息")
    private String message;
    @Schema(description = "响应数据")
    private T data;

    public static <T> Result<T> ok(){
        return Result.setResult(ResultCodeEnum.SUCCESS);
    }
    public static <T> Result<T> fail(){
        return Result.setResult(ResultCodeEnum.ERROR);
    }
    //提供一个静态方法：可以接收枚举类对象给code和message赋值
    public static <T> Result<T> setResult(ResultCodeEnum codeEnum){
        Result result = new Result();
        result.code = codeEnum.getCode();
        result.message = codeEnum.getMessage();
        return result;
    }
    //链式调用方法：修改code message 和data属性值 仍然返回对象自己
    public Result<T> code(Integer code){
        this.code = code;
        return this;
    }
    public Result<T> message(String message){
        this.message = message;
        return this;
    }
    public Result<T> data(T data){
        this.data = data;
        return this;
    }

}
